class Solution {
public:
    int where = 0;
    string path;
    string decodeString(string s) {
        f(s, 0);
        return path;
    }

    string f(string& st, int i)
    {
        int cnt = 0;
        while (i < st.size() && st[i] != ']')
        {
            if (st[i] >= 'a' && st[i] <= 'z' || st[i] >= 'A' && st[i] <= 'Z')
                path.push_back(st[i++]);
            else if (st[i] >= '0' && st[i] <= '9')
                cnt = cnt * 10 + st[i++] - '0';
            else
            {
                path.push_back(get(cnt, f(st, i + 1)));
                i = where + 1;
                cnt = 0;
            }
        }
        where = i;
        return path;
    }

    string get(int n, const string& s)
    {
        string ps;
        while (n--)
        {
            ps.append(s);
        }
        return ps;
    }
};
